<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/26
 * Time: 16:19
 */

namespace app\admin\controller;

use app\model\RuleGroup as RuleGroupModel;
use app\model\Rule as RuleModel;
use app\model\Role as RoleModel;
use app\model\Auth as AuthModel;
use app\validate\RuleValidate;

class Rule
{
    public function addRuleGroup(){
        if(request()->isPost()){
            $data = request()->only('rule_group_name,rule_group_info');
            $RuleGroupModel = new RuleGroupModel();
            $check = $RuleGroupModel->checName($data['rule_group_name']);
            if($check) return res(0,'已存在该分组名称');
            $data['rule_add_time'] = time();
            if($RuleGroupModel->add($data)){
                return res(1,'添加成功');
            }
            return res(0,'添加失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function getRuleGroup(){
        if(request()->isPost()){
            $RuleGroupModel = new RuleGroupModel();
            $data = $RuleGroupModel->getAll();
            return res(1,'',$data);
        }
        return res(0,MSG_NOT_POST);
    }
    public function delRuleGroup(){
        if(request()->isPost()){
            $rule_group_id = input('rule_group_id');
            $RuleGroupModel = new RuleGroupModel();
            $check_has = $RuleGroupModel->getById($rule_group_id);
            if($check_has){
                $res = $check_has->delete();
                if($res){
                    return res(1,'删除成功');
                }
                return res(0,'删除失败');
            }
            return res(0,'不存在该分组');
        }
        return res(0,MSG_NOT_POST);
    }
    public function editRuleGroup(){
        if(request()->isPost()){
            $data = request()->only('rule_group_name,rule_group_info');
            $rule_group_id = input('rule_group_id');
            $RuleGroupModel = new RuleGroupModel();
            $check_has = $RuleGroupModel->getById($rule_group_id);
            if(!$check_has) return res(0,'无该分组');
            if(isset($data['rule_group_name'])) {
                $check = $RuleGroupModel->checName($data['rule_group_name']);
                if ($check) return res(0, '已存在该分组名称');
            }
            $edit_res = $RuleGroupModel->edit($rule_group_id,$data);
            if($edit_res){
                return res(1,'修改成功');
            }
            return res(0,'修改失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function addRule(){
        if(request()->isPost()){
            $data = request()->only('rule_name,rule_info,rule_key,rule_group_id');
            $RuleValidate = new RuleValidate();
            if(!$RuleValidate->check($data)) return res(0,$RuleValidate->getError());
            $RuleGroupModel = new RuleGroupModel();
            $check_group_has = $RuleGroupModel->getById($data['rule_group_id']);
            if(!$check_group_has) return res(0,'不存在该分组');
            $RuleModel = new RuleModel();
            $check_name = $RuleModel->checkName($data['rule_name']);
            if($check_name) return res(0,'已存在同名规则');
            $check_key = $RuleModel->checkKey($data['rule_key']);
            if($check_key) return res(0,'已存在同名规则标识');
            $data['rule_time'] = time();
            $add_res = $RuleModel->add($data);
            if($add_res) return res(1,'添加成功',$add_res);
            return res(0,'添加失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function getRule(){
        if(request()->isPost()){
            $RuleModel = new RuleModel();
            $data = $RuleModel->getAllByPage();
            return res(1,'',$data);
        }
        return res(0,MSG_NOT_POST);
    }
    public function delRule(){
        if(request()->isPost()){
            $rule_id = input('rule_id');
            $RuleModel = new RuleModel();
            $rule = $RuleModel->getById($rule_id);
            if(!$rule) return res(0,'规则不存在');
            $del_res = $rule->delete();
            if($del_res) return res(1,'删除成功');
            return res(0,'删除失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function editRule(){
        if(request()->isPost()){
            $data = request()->only('rule_name,rule_info,rule_key,rule_group_id');
            $rule_id = input('rule_id');
            if(isset($data['rule_group_id'])) {
                $RuleGroupModel = new RuleGroupModel();
                $check_group_has = $RuleGroupModel->getById($data['rule_group_id']);
                if (!$check_group_has) return res(0, '不存在该分组');
            }
            $RuleModel = new RuleModel();
            if(isset($data['rule_name'])) {
                $check_name = $RuleModel->checkName($data['rule_name']);
                if ($check_name) return res(0, '已存在同名规则');
            }
            if(isset($data['rule_key'])) {
                $check_key = $RuleModel->checkKey($data['rule_key']);
                if ($check_key) return res(0, '已存在同名规则标识');
            }
            $edit_res = $RuleModel->edit($rule_id,$data);
            if($edit_res) return res(1,'修改成功');
            return res(0,'修改失败');
        }
        return res(0,MSG_NOT_POST);
    }

    public function getAllAuth(){
        if(request()->isPost()){
            $AuthModel = new AuthModel();
            $res_data = $AuthModel->getAllAuth();
            return res(1,'',$res_data);
        }
        return res(0,MSG_NOT_POST);
    }
    public function getRoleAuth(){
        if(request()->isPost()){
            $role_id = input('role_id');
            if(!checkNull($role_id)) return res(0,MSG_NOT_KEY);
            $RoleModel = new RoleModel();
            $role = $RoleModel->getById($role_id);
            if(!$role) return res(0,'无该角色');
            $AuthModel = new AuthModel();
            $res_data = $AuthModel->getByRole($role_id);
            return res(0,'',$res_data);
        }
        return res(0,MSG_NOT_POST);
    }
    public function getGroupAndRole(){
        if(request()->isPost()){
            $RuleGroupModel = new RuleGroupModel();
            $RuleModel = new RuleModel();
            $res_data = $RuleGroupModel->getAll();
            foreach ($res_data as $index => $item){
                $res_data[$index]['rules'] = $RuleModel->getByGroup($item['rule_group_id']);
            }
            return res(1,'',$res_data);
        }
        return res(0,MSG_NOT_POST);
    }
    public function getRuleAuth(){
        if(request()->isPost()){
            $rule_id = input('rule_id');
            if(!checkNull($rule_id)) return res(0,MSG_NOT_KEY);
            $AuthModel = new AuthModel();
            $res_data = $AuthModel->getByRule($rule_id);
            return res(0,'',$res_data);
        }
        return res(0,MSG_NOT_POST);
    }
    public function grantAuth(){
        if(request()->isPost()){
            $data = request()->only('rule_id,role_id');
            if(!isset($data['rule_id']) || !isset($data['role_id'])) return res(0,MSG_PARAM);
            $AuthModel = new AuthModel();
            $auth_has = $AuthModel->check($data['role_id'],$data['rule_id']);
            if($auth_has) return res(0,'不可授予相同权限');
            $RoleModel = new RoleModel();
            $role_has = $RoleModel->getById($data['role_id']);
            if(!$role_has) return res(0,'无该角色');
            $RuleModel = new RuleModel();
            $rule_has = $RuleModel->getById($data['rule_id']);
            if(!$rule_has) return res(0,'无该规则');
            $data['auth_time'] = time();

            $grant_res = $AuthModel->add($data);
            if($grant_res) return res(1,'授予权限成功');
            return res(0,'授予权限失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function revokeAuth(){
        if(request()->isPost()){

            $data = request()->only('rule_id,role_id');
            if(!isset($data['rule_id']) || !isset($data['role_id'])) return res(0,MSG_PARAM);
            $AuthModel = new AuthModel();
            $auth = $AuthModel->check($data['role_id'],$data['rule_id']);
            if($auth){
                $revoke_auth = $auth->delete();
                if($revoke_auth) {
                    return res(1,'撤销权限成功');
                }
                return res(0,'撤销权限失败');
            }
            return res(0,'无该权限项');
        }
        return res(0,MSG_NOT_POST);
    }
    public function revokeAuthById(){
        if(request()->isPost()){
            $auth_id = input('auth_id');
            if(!checkNull($auth_id)) return res(0,MSG_NOT_KEY);
            $AuthModel = new AuthModel();
            $auth = $AuthModel->getById($auth_id);
            if($auth){
                $revoke_auth = $auth->delete();
                if($revoke_auth) {
                    return res(1,'撤销权限成功');
                }
                return res(0,'撤销权限失败');
            }
            return res(0,'无该权限项');
        }
        return res(0,MSG_NOT_POST);
    }

}